﻿package su.fishr.social_network.OK.uploadV2.utils {

	import flash.utils.ByteArray;
	import flash.utils.Endian;

	/** 
	* @private 
	*  Создает форматрированную строку бинарных данных для
	* передачи в запросе на загрузку фото.
	*/ 
	public class PostRequest {

		/**
		 * @private
		 *
		 */
		public var boundary:String = '-----';

		/**
		 * @private
		 *
		 */
		protected var postData:ByteArray;


		/**
		 * Instantiates a new PostRequest instance.
		 *
		 */
		public function PostRequest() {
			createPostData();
		}

		/**
		 * Starts a new request.
		 *
		 */
		public function createPostData():void {
			postData = new ByteArray();
			postData.endian = Endian.BIG_ENDIAN;
		}

		/**
		 * Writes a new value to the current POST request.
		 *
		 * @param name Name of the new value.
		 * @param value String value to write to the POST data.
		 *
		 */
		public function writePostData(name:String, value:String):void {
			var bytes:String;

			writeBoundary();
			writeLineBreak();

			bytes = 'Content-Disposition: form-data; name="' + name + '"';

			var l:uint = bytes.length;
			for (var i:Number=0; i<l; i++)  {
				postData.writeByte( bytes.charCodeAt(i) );
			}

			writeLineBreak();
			writeLineBreak();

			postData.writeUTFBytes(value);

			writeLineBreak();
		}

		/**
		 * Writes a ByteArray to the POST request.
		 *
		 * @param filename Name of the file data to upload.
		 * @param fileData Raw byte array of file data to upload.
		 * @contentType Content type of data being uploaded (ex. ContentType: image/png).
		 *
		 */
		public function writeFileData(
			filename:String,
			fileData:ByteArray,
			contentType:String):void {

			var bytes:String;
			var l:int;
			var i:uint;

			writeBoundary();
			writeLineBreak();

			bytes = 'Content-Disposition: form-data; name="'+filename+'"; filename="';
			l = bytes.length;

			for (i=0; i<l; i++)  {
				postData.writeByte(bytes.charCodeAt(i));
			}

			postData.writeUTFBytes(filename+".JPG");

			writeQuotationMark();
			writeLineBreak();

			bytes = contentType;
			l = bytes.length;
			for (i=0; i<l; i++) {
				postData.writeByte(bytes.charCodeAt(i));
			}

			writeLineBreak();
			writeLineBreak();

			fileData.position = 0;
			postData.writeBytes(fileData, 0, fileData.length);

			writeLineBreak();
			
			fileData.clear();
		}

		/**
		 * Returns the POST bytes to upload.
		 *
		 */
		public function getPostData():ByteArray {
			postData.position = 0;
			return postData;
		}

		/**
		 * Closes this request. This method must be called last.
		 *
		 */
		public function close():void {
			writeBoundary();
			writeDoubleDash();
		}

		/**
		 * @private
		 *
		 */
		protected function writeLineBreak():void {
			postData.writeShort(0x0d0a);
		}

		/**
		 * @private
		 *
		 */
		protected function writeQuotationMark():void  {
			postData.writeByte(0x22);
		}

		/**
		 * @private
		 *
		 */
		protected function writeDoubleDash():void {
			postData.writeShort(0x2d2d);
		}


		/**
		 * @private
		 *
		 */
		protected function writeBoundary():void  {
			writeDoubleDash();

			var l:uint = boundary.length;
			for (var i:uint=0; i<l; i++)  {
				postData.writeByte(boundary.charCodeAt(i));
			}
		}

	}
}